package com.cswangdao.controller.admin;

import com.cswangdao.domain.dto.StoragePageDTO;
import com.cswangdao.domain.po.Storage;
import com.cswangdao.result.PageResult;
import com.cswangdao.result.Result;
import com.cswangdao.service.StorageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RequestMapping("/admin/storage")
@RestController
@Slf4j
public class AdminStorageController {


    @Autowired
    private StorageService storageService;

    /**
     * @description: 下载文件
     * @param: req, resp
     * @return:
     */

//    @GetMapping("/fetch/{uuid}")
//    public void fetch(@PathVariable("uuid") String uuid, HttpServletResponse resp) {
//        log.info("下载文件：{}", uuid);
//        storageService.dowanload(uuid, resp);
//
//    }

    /**
     * @description: 上传文件
     * @param: req, resp
     * @return:
     */

    @PostMapping("/create")
    public Result<Storage> create(MultipartFile file) throws IOException {
        log.info("上传文件：{}", file.getOriginalFilename());
        Storage upload = storageService.upload(file);
        return Result.success(upload);
    }

    @GetMapping("/list")
    public Result<PageResult<Storage>> list(StoragePageDTO pageDTO) {
        log.info("查询文件列表：{}", pageDTO);
        PageResult<Storage> pageResult = storageService.pageQueryInfo(pageDTO);
        return Result.success(pageResult);
    }

    @PostMapping("/delete")
    public Result delete(@RequestBody Storage storage) {
        log.info("删除文件信息：{}", storage);
        storageService.deleteById(storage.getId());
        return Result.success();
    }

    @PostMapping("/update")
    public Result<Storage> update(@RequestBody Storage storage) {
        log.info("更新文件信息：{}", storage);
        storageService.updateById(storage);
        return Result.success(storage);
    }


}
